const fs = require('fs');
const readline = require('linebyline');
const _ = require('lodash');
const name = 'maidou-2';
// const name = 'dongchen0502';

let filePath = '/Users/dongchen/Downloads/orders.csv';
// let result = fs.readFileSync(filePath, 'utf8');
let rl = readline(filePath);

let tmp = {};
rl.on('line', function(line, lineCount, byteCount) {
    if(lineCount == 1){
        return;
    }
    let arr = line.split(',');
    let coinType = arr[9];
    let cache = tmp[coinType];
    if(!cache){
        cache = tmp[coinType] = [];
    }
    cache.push(arr);
}).on('error', function(e) {
        console.log(e);
}).on('end' , function(){
    for(let type in tmp){
        let total_out = 0;
        console.log(type);

        let money = 0;
        let ecoin = 0;
        let t_proxy = 0;

        let orderArr = _.orderBy(tmp[type], 12);

        for(let i in orderArr){
            let arr = orderArr[i];
            let time = arr[12];
            if(time < '2018-02-06 22:38'){
                continue;
            }
            let type = (arr[4] === name) ? '买' : '卖';
            let amount = +arr[6];
            let Eamount = +arr[8];
            let proxy = +arr[10];

            if(amount >= 5000 && proxy > 0){
                // console.log('-------------------------------------------------------------->大单', amount, arr[4], arr[5]);
            }
            if(type === '卖'){
                // console.log(+i + 1, '<<<<<<<<<<<<<<<<<<<<<<<<<<---------');
                money -= amount;
                ecoin -= Eamount;
            }else if(type === '买'){
                // console.log(+arr[11], '--------->>>>>>>>>>>>>>>>>>>>>>>>>>', Eamount);
                money += amount;
                ecoin += Eamount;
            }
            // ecoin -= proxy;
            t_proxy += proxy;
            // console.log(line);
            let out = Math.round(money * 100) / 100;
            let avg = out / ecoin;
            console.log(`${arr[12]}, ${type}, 单价${(+arr[11]).toFixed(2)}，共支出${out}元，持有货币${arr[9]} = ${ecoin}枚，均价为${avg}元/个`);
            // console.log(`共产生手续费${t_proxy} ${arr[9]}, 提成 ${t_proxy * 0.2}`);
        }
        total_out += money;
        console.log(`共支出${total_out}元`);
    }

});
